package leetCode;

public class CanIWin {
	boolean[] takenByA;
    public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
        boolean[] taken=new boolean[maxChoosableInteger+1];
        takenByA=new boolean[maxChoosableInteger+1];
        
        boolean res= canIWin(desiredTotal,taken,0);
        for (boolean b : takenByA) {
			System.out.println(b);
		}
        return res;
    }

	private boolean canIWin(int desiredTotal, boolean[] taken,int person) {
		for (int i = 1; i < taken.length; i++) {
			if(!taken[i]&&i>=desiredTotal){
				System.out.println(person +"拿走"+i+"成功");
				if(person==0){
					takenByA[i]=true;
				}
				return true;
			}
		}
		boolean result=false;
		for (int i = 1; i < taken.length; i++) {
			if(!taken[i]){//如果没有拿走过
				taken[i]=true;
				if(person==0){
					takenByA[i]=true;
				}
				System.out.println(person +"拿走"+i);
				boolean enemy=canIWin(desiredTotal-i,taken,1-person);
				if(!enemy){
					return true;
				}
				taken[i]=false;
			}
		}
		return false;
	}
	
	public static void main(String[] args) {
		CanIWin canIWin=new CanIWin();
		System.out.println(canIWin.canIWin(10, 40));
	}
}
